<?php
defined('SYSPATH') OR die('No direct access allowed.');
/**
 * lowongan.php
 * @author ranuandra
 *
 * 13 Mar 2012 13:34:49
 *
 * 
 */

class Controller_Admin_Lowongan extends Kohana_Admin
{
	
	private $_arealist = array();
	private $_placelist = array();
	private $_departmentlist = array();
	private $_pendidikanlist = array();
	private $_jabatanlist = array();
	
	public function action_index()
	{
		//$datas = ORM::factory('mstlowongan')->where('mstproses_id', 'IS', NULL)->find_all();
		$datas = ORM::factory('mstlowongan')->find_all();
		
		$view = View::factory('admin/lowongan/index');
		$view->datas = $datas;

		$this->template->container = $view;
	}
	
	public function action_detail()
	{
		$id = Request::current()->param('id');
		if($id != "")
		{
			$proseses = ORM::factory('mstproses')->order_by('urutan', 'ASC')->find_all();
			$lowongan = ORM::factory('mstlowongan')->where('id', '=', $id)->find();
			$view = View::factory('admin/lowongan/detail');
			$view->data = $lowongan;
			$view->proseses = $proseses;
			$this->template->container = $view;
		}

	}
	private function _createlist()
	{
		
		$this->_arealist = ORM::factory('mstareaofwork')->find_all()->as_array('id', 'areaofwork');
		$this->_placelist = ORM::factory('mstplacement')->find_all()->as_array('id', 'placement');
		$this->_departmentlist = ORM::factory('mstdepartment')->find_all()->as_array('id', 'department');
		$this->_pendidikanlist = ORM::factory('mstpendidikan')->find_all()->as_array('id', 'pendidikan');
		$this->_jabatanlist = ORM::factory('mstjabatan')->find_all()->as_array('id', 'jabatan');
	}
	
	public function action_new()
	{
		$this->_createlist();
		$form = ORM::factory('mstlowongan')->getForm();
		$view = View::factory('admin/lowongan/form');

		$view->form = $form;
		$view->arealists = $this->_arealist;
		$view->placelists = $this->_placelist;
		$view->departmentlists = $this->_departmentlist;
		$view->pendidikanlists = $this->_pendidikanlist;
		$view->jabatanlists = $this->_jabatanlist;
		$this->template->container = $view;
	}
	
	public function action_edit()
	{
		$id = Request::current()->param('id');
		$data = ORM::factory('mstlowongan', $id)->as_array();
		$view = View::factory('admin/lowongan/form');
		$view->form = $data;
		$this->template->container = $view;		
	}
	
	public function action_inactive()
	{
		$id = Request::current()->param('id');
		if($id)
		{
			$this->_setFlag($id, 0);
			Request::current()->redirect(URL::base().'admin/lowongan');
			exit;
			
		}
	}
	
	public function action_active()
	{
		$id = Request::current()->param('id');
		if($id)
		{
			$this->_setFlag($id, 1);
			Request::current()->redirect(URL::base().'admin/lowongan');
			exit;			
		}
	}

	
	private function _setFlag($id, $flag)
	{
			$db = Database::instance();
			$db->begin();
			
			$data = ORM::factory('mstlowongan', $id);
			try {
				$data->flag = $flag;
				$data->save();
				$db->commit();
				
			}
			catch(ORM_Validation_Exception $e)
			{
				$db->rollback();
				$errors = $e->errors('models');
				print_r($errors);
			}		
	}
	
	
	public function action_save()
	{

		if(isset($_POST['submit']))
		{
			$db = Database::instance();
			$db->begin();
			
			try 
			{
				if(isset($_POST['id']))
				{
					$lowongan = ORM::factory('mstlowongan', Request::current()->post('id'));
					$lowongan->mstareaofwork_id = Request::current()->post('mstareaofwork_id');
					$lowongan->mstplacement_id = Request::current()->post('mstplacement_id');
					$lowongan->datefrom = Request::current()->post('datefrom');
					$lowongan->dateto = Request::current()->post('dateto');
					$lowongan->mstdepartment_id = Request::current()->post('mstdepartment_id');
					$lowongan->deskripsi = Request::current()->post('deskripsi');
					$lowongan->mstpendidikan_id = Request::current()->post('mstpendidikan_id');
					$lowongan->kualifikasi_umum = Request::current()->post('kualifikasi_umum');
					$lowongan->kualifikasi_khusus = Request::current()->post('kualifikasi_khusus');
					$lowongan->areaofwork_id = Request::current()->post('areaofwork_id');
					$lowongan->publisheddate = Request::current()->post('publisheddate');
					$lowongan->mstjabatan_id = Request::current()->post('mstjabatan_id');
					$lowongan->arsip = Request::current()->post('arsip');
					
					//$lowongan->flag = Request::current()->post('flag');
					$lowongan->save();
				}
				else 
				{
					$year = date("Y");
					$month = date("m");
					$mx = DB::select(array('MAX("id")', 'maks'))->from('mstlowongans')->execute();

					$no = $mx[0]['maks'] + 1;
					$lowongan = ORM::factory('mstlowongan');
					$lowongan->kode = "HRD-".$year.$month."-".$no;
					$lowongan->select('');
					$lowongan->create_data($_POST);
					
					$placements = Request::current()->post('mstplacements');
					if(count($placements)> 0)
					{
						foreach($placements as $placement)
				  		{
				  			
				  			$lowongan_placement = new Model_Mstplacement(array('id' => $placement));
				  			$lowongan->add('mstplacements', $lowongan_placement);
				  		}						
					}	
				}
				
				$db->commit();
				Request::current()->redirect(URL::base().'admin/lowongan');
				exit;
			}
			catch(ORM_Validation_Exception $e)
			{
				$db->rollback();
				$errors = $e->errors('models');
				print_r($errors);
			}
		}
	}		
}